package com.miyake.loveviewweb.client.viewer;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class Viewer extends Composite {
    protected int getLevel() {
        return 0;
    }
    private boolean toolBarVisible = true;
    abstract String getPageTitle();
    abstract Widget getToolbar();
    abstract Widget getContent();
    
    private VerticalPanel panel = new VerticalPanel();
    public Viewer() {
        initWidget(panel);
        panel.setWidth("100%");
    }
    
    public void update(Key key) {
        panel.clear();
        Widget widgetTitle = createTitle();
        if(widgetTitle != null) {
            panel.add(widgetTitle);
        }
        
        if (isToolBarVisible()) {
            Widget widgetToolbar = getToolbar();


            if (widgetToolbar != null) {
                SimplePanel s = new SimplePanel();
                s.setWidth("100%");
                s.setStylePrimaryName("toolbar");
                s.add(widgetToolbar);
                panel.add(s);
            }
        }
        
        Widget widgetContent = createContent();
        if (widgetContent != null) {
            panel.add(widgetContent);
        }
    }
    
    private Widget createContent() {
        return getContent();
    }
    
    private Widget createTitle() {
        Integer level = getLevel() + 1;
        String html = "<h" + level.toString() + ">" + getPageTitle() + "<h" + level.toString() + "/>";
        return new HTML(html);
    }
    /**
     * @return the toolBarVisible
     */
    public boolean isToolBarVisible() {
        return toolBarVisible;
    }
    /**
     * @param toolBarVisible the toolBarVisible to set
     */
    public void setToolBarVisible(boolean toolBarVisible) {
        this.toolBarVisible = toolBarVisible;
    }
}
